package com.kj.report.service.impl;

import com.kj.report.mapper.InspectResultBaseInfoMapper;
import com.kj.report.model.HashMapResultVo;
import com.kj.report.service.InspectResultBaseInfoService;
import com.kj.report.utils.Constants;
import com.kj.report.utils.DateUtil;
import com.kj.report.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;




@Service("inspectResultBaseInfoService")
public class InspectResultBaseInfoServiceImpl implements InspectResultBaseInfoService {

	@Resource
	private RedisUtils redisUtils;

	@Resource
	private InspectResultBaseInfoMapper inspectResultBaseInfoMapper;

	@Override
	public Integer rankingUserScore(Integer totalScore, Integer reportType, Integer age) {
		//没有年龄不计算排名
		if(age==null || age==0){
			return 0;
		}
		String field="";
		boolean flag=true;
		Integer total=0;
		Integer greaterTotal=0;
		String key= Constants.REDIS_RANKING_KEY+reportType;
		Map<String, Object> params=new HashMap<String, Object>();
		if(reportType!=null){
			params.put("reportType", reportType);
			AgeGroup group = new AgeGroup(age);
			int min = group.getMin();
			int max = group.getMax();
			params.put("min", min);
			params.put("max", max);
			field=group.getKey();
		}
		List<HashMapResultVo> hashMapResultVoList= redisUtils.getListCache(key+field, HashMapResultVo.class);
		if(hashMapResultVoList==null ||hashMapResultVoList.size()<=0){

			hashMapResultVoList=inspectResultBaseInfoMapper.findRankingData(params);
		}
		for (int i=0;i<hashMapResultVoList.size();i++) {
			HashMapResultVo hashMapResultVo=hashMapResultVoList.get(i);
			if(totalScore> hashMapResultVo.getKey() ){
				greaterTotal+=hashMapResultVo.getValue();
			}
			total+=hashMapResultVo.getValue();
			if(totalScore.equals(hashMapResultVo.getKey())){
				flag=false;
				Integer value=hashMapResultVo.getValue()+1;
				hashMapResultVo.setValue(value);
				hashMapResultVoList.set(i, hashMapResultVo);
				redisUtils.putListCacheWithExpireTime(key+field, hashMapResultVoList, DateUtil.getEndTime());
			}
		}
		if(flag){
			HashMapResultVo hashMapResultVo=new HashMapResultVo();
			hashMapResultVo.setKey(totalScore);
			hashMapResultVo.setValue(1);
			hashMapResultVoList.add(hashMapResultVo);
			redisUtils.putListCacheWithExpireTime(key+field, hashMapResultVoList,DateUtil.getEndTime());
		}
		double rankingTemp = greaterTotal.doubleValue()/total.doubleValue()*100;
		return  (int)Math.round(rankingTemp);
	}

	private class AgeGroup {
		private int max;
		private int min;
		private String key;

		public AgeGroup(int age) {
			super();
			this.init(age);
		}

		public void init(int age) {
			if (age < 30) {
				min = 0;
				max = 29;
				key="0029";
			} else if (age >= 30 && age < 40) {
				min = 30;
				max = 39;
				key="3039";
			} else if (age >= 40 && age < 50) {
				min = 40;
				max = 49;
				key="4049";
			} else if (age >= 50 && age < 60) {
				min = 50;
				max = 59;
				key="5059";
			} else {
				min = 60;
				max = 99;
				key="6099";
			}
		}

		public int getMax() {
			return max;
		}

		public int getMin() {
			return min;
		}

		public String getKey() {
			return key;
		}
	}
}
